package com.ozner.cup.Device.Kettle.KettleRemindUtils;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;

import com.ozner.cup.Device.ReplenWater.RemindUtils.RemindUtil;
import com.ozner.cup.R;
import com.ozner.cup.Utils.LCLogUtils;

import java.util.Calendar;
import java.util.Date;

/**
 * description: OznerAlarmReceiver
 * autour: ozner_67
 * date: 2017/7/21 17:59
 * e-mail: xinde.zhang@cftcn.com
 */
public class OznerAlarmReceiver extends BroadcastReceiver {
    private static final String TAG = "OznerAlarmReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        long interval = intent.getLongExtra(OznerRemindUtil.INTERVAL_MILLIS, 24 * 3600);
        int reqCode = intent.getIntExtra(OznerRemindUtil.PARMS_REQ_CODE, OznerRemindUtil.REQ_CODE_KETTLE);
        long start = intent.getLongExtra(OznerRemindUtil.PARMS_START_TIMEINMILLIS, -1);
        long end = intent.getLongExtra(OznerRemindUtil.PARMS_END_TIMEINMILLIS, -1);
        Log.e(TAG, "onReceive: interval:" + interval + " ,reqCode:" + reqCode + " ,开始时间：" + new Date(start).toLocaleString());

        if (start < 0) {
            OznerRemindUtil.getInstance(context).stopRemind(reqCode);
            return;
        }

        Calendar curCal = Calendar.getInstance();
        if (end != -1 && curCal.getTimeInMillis() > end) {
            Log.e(TAG, "onReceive: 超过当前时间段");
            Calendar startCal = Calendar.getInstance();
            startCal.setTimeInMillis(start);
            startCal.add(Calendar.DAY_OF_MONTH, 1);
            Calendar endCal = Calendar.getInstance();
            endCal.setTimeInMillis(end);
            endCal.add(Calendar.DAY_OF_MONTH, 1);
            OznerRemindUtil.getInstance(context).stopRemind(reqCode);
            OznerRemindUtil.getInstance(context).startRemind(reqCode, startCal, endCal, interval);
            return;
        } else {
            LCLogUtils.E(TAG, "onReceive:提醒服务执行 interval:" + interval + " , action:" + intent.getAction());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.MILLISECOND, 0);
                AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

                PendingIntent sender = PendingIntent.getBroadcast(context, reqCode,
                                                                  intent, PendingIntent.FLAG_CANCEL_CURRENT);
                am.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis() + interval, sender);
            }
            showNotify(context, reqCode);

//        long interval = intent.getLongExtra(RemindUtil.INTERVAL_MILLIS, 0);
//        int reqCode = intent.getIntExtra(RemindUtil.PARMS_REQ_CODE, 1);
//        LCLogUtils.E(TAG, "onReceive:提醒服务执行 interval:" + interval + " , action:" + intent.getAction());
//        LCLogUtils.E(TAG, "onReceive: action equal: " + intent.getAction().equals(OznerRemindUtil.ALARM_ACTION));
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            Calendar cal = Calendar.getInstance();
//            cal.set(Calendar.MILLISECOND, 0);
//            AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
//
//            PendingIntent sender = PendingIntent.getBroadcast(context, reqCode,
//                                                              intent, PendingIntent.FLAG_CANCEL_CURRENT);
//            am.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis() + interval, sender);
//        }
//        showNotify(context, reqCode);
        }
    }

    private void showNotify(Context context, int reqCode) {
        Notification.Builder builder = new Notification.Builder(context);
        Intent intent = new Intent(context, KettleNotifyActivity.class);
        intent.putExtra(RemindUtil.PARMS_REQ_CODE, reqCode);
        PendingIntent pi = PendingIntent.getActivity(context, reqCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        builder.setSmallIcon(R.mipmap.ozner)
                .setAutoCancel(true)
                .setContentText("该喝水了")//消息内容
                .setContentIntent(pi)
                .setContentTitle("电热壶饮水提醒")
                .setTicker("ticker")
                .setDefaults(Notification.DEFAULT_ALL);
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(reqCode, builder.build());
    }
}
